//
//  XRDataRequestManger.m
//  YWBaseFrameWork
//
//  Created by 余伟 on 2017/1/13.
//  Copyright © 2017年 NeiQuan. All rights reserved.
//

#import "YeeDataRequestManger.h"
#import "AFNetworking.h"
#import "YeeNetClient.h"
#import "PINCache.h"
@implementation YeeDataRequestManger
+ (void)postJSONWithUrl:(NSString *)urlStr
             parameters:(id)parameters
                  cache:(BOOL)cache
          responseCache:(CacheBlock)responseCache
                success:(SuccessBlock)success
                   fail:(FailBlock)fail
{
    
    AFHTTPSessionManager *manger = [AFHTTPSessionManager manager];
    manger.responseSerializer    = [AFHTTPResponseSerializer serializer];
    manger.requestSerializer.timeoutInterval = 20.f;
    manger.requestSerializer = [AFJSONRequestSerializer serializer];
   // [manger.requestSerializer setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];

    NSMutableDictionary *mDic = [NSMutableDictionary dictionaryWithDictionary:parameters];
    NSString *cacheUrl=[NSString stringWithFormat:@"%@%@",urlStr,parameters];
    if (responseCache)
    {
        responseCache([[PINCache sharedCache] objectForKey:cacheUrl]);
    }
    
    [manger POST:urlStr parameters:mDic progress:^(NSProgress * _Nonnull uploadProgress)
     {
       
         
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject)
    {
        if (cache)
        {
        [ [PINCache sharedCache] setObject:responseObject forKey:cacheUrl];
        }
        if (success)
        {
            success(responseObject);
        }
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error)
    {
        if (fail)
        {
            fail(error);
        }
    }];
}
//get获取数据
+ (void)getJSONWithUrl:(NSString *)urlStr
            parameters:(id)parameters
                 cache:(BOOL)cache
         responseCache:(CacheBlock)responseCache
               success:(SuccessBlock)success
                  fail:(FailBlock)fail
{
    AFHTTPSessionManager *manger = [AFHTTPSessionManager manager];
    manger.responseSerializer    = [AFHTTPResponseSerializer serializer];
    manger.requestSerializer.timeoutInterval = 20.f;
    NSMutableDictionary *mDic = [NSMutableDictionary dictionaryWithDictionary:parameters];
    NSString *cacheUrl=[NSString stringWithFormat:@"%@%@",urlStr,parameters];
    if (responseCache)
    {
        responseCache([[PINCache sharedCache] objectForKey:cacheUrl]);
    }

    [manger GET:urlStr parameters:mDic progress:^(NSProgress * _Nonnull downloadProgress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
        NSDictionary *result=[NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:nil];
        
        
        if (cache)
        {
            [[PINCache sharedCache] setObject:result forKey:cacheUrl];
        }
        if (success)
        {
            success(result);
        }

    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
        if (fail)
        {
            fail(error);
        }
    }];
}
@end
